#!/usr/bin/env python
#Version = 3.8.1
# -*- coding: utf-8 -*-
import random, string
import time
import smtplib
import socket
from smtplib import SMTP_SSL
from imaplib import IMAP4
from email.mime.text import MIMEText
from email.header import Header

def send_email(receover, ecode):
    send_auther = "<17633530384@163.com>"
	    text = f"<br/> 服务器端口不通，服务器ip和端口为: <span stype='color: red; font-size:20px;'>{ecode}</span>, 请及时查看服务器"
		    content = text

			    message = MIMEText(content, 'html', 'utf-8')

				    message['subject'] = Header('服务器端口不通', 'utf-8')
					    message['From'] = send_auther  
						    message['To'] = receover 

							#    smtp_server = 'smtp.qq.com'
							    smtp_server = 'smtp.163.com'
								    smtp_port = 465

									    smtpobj = smtplib.SMTP_SSL(smtp_server, smtp_port) 
										    smtpobj.login(user='17633530384', password='KANTDJAICPWUJORR')
											    smtpobj.sendmail(send_auther, receover, str(message))
												    smtpobj.quit() 





													def CheckPort(server_ip, port):
													    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
														    sk.settimeout(30)  

															    try:
																        sk.connect((server_ip, port))

																		        print('OK')

																				    except Exception:
																					        server_info = (server_ip,port)
																							        print(server_info)

																									        send_email('624475316@qq.com',server_info)
																											        send_email('zxf@pobo.net.cn', server_info)
																													        send_email('mcc@pobo.net.cn', server_info)

																															        print('send_email_done')

																																	    sk.close()

																																		# 服务器地址和端口
																																		#ipaddr_237 = {'222.73.7.158': [80, 443, 10000]}
																																		#ipaddr_237 = {'120.253.219.58':[6789]}
																																		#ipaddr_237 = {'120.253.219.58':[20001]}
																																		ipaddr_238 = {'222.73.7.154':[80]}
																																		ipaddr_239 = {'101.226.207.175':[80]}
																																		ipaddr_240 = {'222.73.7.160':[443]}
																																		#
																																		ip_list = [ipaddr_238,ipaddr_239,ipaddr_240]

																																		# 方式一
																																		def nostop(ip_lists):
																																		    # 一直循环telnet列表里ip端口
																																			    while 1:
																																				        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
																																						        for first in ip_lists:
																																								            for k, y in first.items():
																																											                for port in y:
																																															                    print(k,port)
																																																				                    CheckPort(k,port)
																																																									        time.sleep(300)

																																																											if __name__ == "__main__":

																																																											    nostop(ip_list)


